Git and Github

Murray Logan

28 July 2024

Background

Required

  • RStudio or text editor

Version control

Version control

  • large space requirements
  • difficult to collaborate

Git

Git - overview

  • Git - is a distributed version control system
  • Git - stores snapshots of the filesystem
  • commits

  • The system consists of three trees:

Git - overview

Files can be in one of four states:

  • untracked

Git - overview

Files can be in one of four states:

  • untracked
  • staged

Git - overview

Files can be in one of four states:

  • untracked
  • staged
  • committed

Git - overview

Files can be in one of four states:

  • untracked
  • staged
  • committed
  • modified

Git - overview

Git - overview

Git in depth

Git - configuration

This is a once off action (per machine)

  • define who you are
git config --global user.name "Your name"
git config --global user.email "your_email@whatever.com"
  • Specify that the initial branch should be called main rather than master
git config --global init.defaultBranch main
  • Check these settings
git config --global --list
user.name=pcinereus
user.email=i.obesulus@gmail.com
init.defaultbranch=main

Git - configuration

Or within R (via the usethis package)

usethis::use_git_config(user.name='Your name',
               user.email='your_email@whatever.com',
               scope='user')
usethis::git_sitrep()

OR via the gert package

gert::git_config_global()

Git - new repository

mkdir ~/tmp/Test_repo
cd ~/tmp/Test_repo
git init

RStudio

R

library(usethis)
create_project(path='~/path/project_name', rstudio=TRUE)
use_git()

Git - adding content


Create a file (text, code etc)

  • those using R, call it analysis.R
x=seq(1, 10, len = 1)
y=40 * 2 + rnorm(10, 0, 5)
plot(x, y)



Otherwise, create any kind of file (in the folder we just created)

Git - adding content

Stage the changes (add)

git add <file(s)>

For example:

git add analysis.R

RStudio

gert::git_add('analysis.R')

Git - adding content

Git - .gitignore

RStudio

Examples

  • .RData all files ending in .RData
  • .pdf all files ending in .pdf
  • data/ the entire folder called data

Git - committing

git commit -m 'Initial commit'
[main (root-commit) 6820e06] Initial commit
 1 file changed, 3 insertions(+)
 create mode 100644 analysis.R
cd ~/tmp/Test_repo
git status
On branch main
nothing to commit, working tree clean